const fs = require('fs');
const path = require('path');

// 读取各个package.json文件
function readPackageJson(filePath) {
  try {
    const content = fs.readFileSync(filePath, 'utf8');
    return JSON.parse(content);
  } catch (error) {
    console.error(`Error reading ${filePath}:`, error.message);
    return null;
  }
}

// 检查版本兼容性
function checkVersionCompatibility(pkg1, pkg2, pkgName, allowDifferent = false) {
  const version1 = pkg1?.dependencies?.[pkgName] || pkg1?.devDependencies?.[pkgName];
  const version2 = pkg2?.dependencies?.[pkgName] || pkg2?.devDependencies?.[pkgName];
  
  if (version1 && version2 && version1 !== version2) {
    if (allowDifferent) {
      console.log(`ℹ️  ${pkgName}: ${version1} vs ${version2} (允许差异)`);
      return true; // 允许差异
    } else {
      console.log(`⚠️  ${pkgName}: ${version1} vs ${version2}`);
      return false;
    }
  }
  return true;
}

// 主检查函数
function checkCompatibility() {
  console.log('🔍 检查项目兼容性...\n');
  
  const rootPkg = readPackageJson('package.json');
  const vue3Pkg = readPackageJson('vue3-project/package.json');
  const uniappPkg = readPackageJson('uniapp-project/package.json');
  
  if (!rootPkg || !vue3Pkg || !uniappPkg) {
    console.error('❌ 无法读取package.json文件');
    return;
  }
  
  let allCompatible = true;
  
  // 检查关键依赖版本
  const keyDependencies = [
    { name: 'vue', allowDifferent: false },
    { name: 'typescript', allowDifferent: false },
    { name: 'vite', allowDifferent: true }, // Vite版本允许差异
    { name: '@types/node', allowDifferent: false }
  ];
  
  console.log('📋 版本兼容性检查结果：');
  console.log('='.repeat(50));
  
  keyDependencies.forEach(dep => {
    const isCompatible = checkVersionCompatibility(vue3Pkg, uniappPkg, dep.name, dep.allowDifferent);
    if (!isCompatible) {
      allCompatible = false;
    }
  });
  
  console.log('\n' + '='.repeat(50));
  
  if (allCompatible) {
    console.log('✅ 所有依赖版本兼容！');
  } else {
    console.log('❌ 发现版本不兼容问题，请检查上述警告。');
  }
  
  // 检查Vue版本
  const vue3Version = vue3Pkg.dependencies?.vue;
  const uniappVersion = uniappPkg.dependencies?.vue;
  
  console.log('\n📊 Vue版本信息：');
  console.log(`Vue3项目: ${vue3Version}`);
  console.log(`UniApp项目: ${uniappVersion}`);
  
  // 检查TypeScript配置
  console.log('\n📊 TypeScript版本信息：');
  console.log(`Vue3项目: ${vue3Pkg.devDependencies?.typescript}`);
  console.log(`UniApp项目: ${uniappPkg.devDependencies?.typescript}`);
  
  // 检查Vite版本
  console.log('\n📊 Vite版本信息：');
  console.log(`Vue3项目: ${vue3Pkg.devDependencies?.vite}`);
  console.log(`UniApp项目: ${uniappPkg.devDependencies?.vite}`);
  console.log('ℹ️  Vite版本差异是正常的：Vue3项目使用Vite 5.x，UniApp项目使用Vite 4.x（兼容性要求）');
  
  // 检查UniApp特定依赖
  console.log('\n📊 UniApp依赖信息：');
  console.log(`@dcloudio/vite-plugin-uni: ${uniappPkg.devDependencies?.['@dcloudio/vite-plugin-uni']}`);
  console.log(`@dcloudio/uni-app: ${uniappPkg.dependencies?.['@dcloudio/uni-app']}`);
  
  console.log('\n💡 说明：');
  console.log('- Vue3项目使用最新的Vite 5.x以获得最佳性能');
  console.log('- UniApp项目使用Vite 4.x以保持与@dcloudio/vite-plugin-uni的兼容性');
  console.log('- 这种版本差异不会影响开发体验，两个项目可以独立运行');
}

// 运行检查
checkCompatibility(); 